package com.uhu.code;

/**
 * 189.轮转数组
 * https://leetcode.cn/problems/rotate-array/description/?envType=study-plan-v2&envId=top-100-liked
 */
public class Solution189 {
    public void rotate(int[] nums, int k) {
        reverse(nums, 0, nums.length - 1);
        reverse(nums, 0, k%nums.length -1);
        reverse(nums, k%nums.length, nums.length - 1);
    }

    private void reverse(int[] nums, int start, int end) {
        if(end <= start) {
            return;
        }
        int tl = (end + start)%2;
        int tp = (end + start)/2;
        if(tl != 0) {
            // 不存在中位数
            tl = 1;
        }
        for(int i = start; i < tp + tl; i++) {
            int temp = nums[2*tp + tl - i];
            nums[2*tp + tl - i] = nums[i];
            nums[i] = temp;
        }
    }
}
